<?php

namespace App\models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Member;

class Share extends Model
{
    protected $table = "redf";

    /**
     * 返回所有的分享的红包
     * @return [type] [description]
     */
    public static function listAll() {
        $shareList = array();
        $fields = ['id','order_sn','uid','openid','number','s_number','sign'];
        foreach (self::where([["is_share",'=',1],['status','=',1]])->orderBy("s_number","desc")->orderBy("created_at","desc")->
        select($fields)->paginate(15) as $share) {
            $shareList[] = [
                'id' => $share->id,
                'orderSn' => $share->order_sn,
                'uid' => $share->uid,
                'openid' => $share->openid,
                'number' => $share->number,
                's_number' => $share->s_number,
                'title' => $share->sign ==1 ? "语音口令" : "猜对有钱",
                'sign' =>  $share->sign
            ];
        }
        $memberIds = array_unique(array_column($shareList,"uid"));
        //查询所有会员信息
        $memberInfoList = Member::listMember($memberIds);
        //组装信息
        foreach ($shareList as &$item) {
            if (isset($memberInfoList[$item['uid']])) {
                $item['nickName'] = $memberInfoList[$item['uid']]['username'];
                $item['avatarUrl'] = $memberInfoList[$item['uid']]['head_ico'];
            }
        }
        return $shareList;
    }

}
